package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.exception.SgpayException;
import com.sgzn.sgpay.model.request.SgsUserInfoQueryRequest;
import com.sgzn.sgpay.model.request.SgsUserMerchantDiscountListRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.SgsUserInfoQueryResponse;
import com.sgzn.sgpay.model.response.vo.UserMerchantDiscountVo;
import com.sgzn.sgpay.persistence.dao.TUserAccountMapper;
import com.sgzn.sgpay.persistence.dao.TUserMapper;
import com.sgzn.sgpay.persistence.domain.TUser;
import com.sgzn.sgpay.persistence.domain.TUserAccount;
import com.sgzn.sgpay.persistence.domain.TUserAccountExample;
import com.sgzn.sgpay.persistence.domain.TUserExample;
import com.sgzn.sgpay.service.SgsUserInfoService;
import com.sgzn.sgpay.service.SgsUserMerchantDiscountService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 查询用户信息服务接口实现类
 * @author huangsenguang
 * @version Id: SgsUserInfoServiceImpl.java, v 0.1 2018/4/22 11:40 huangsenguang Exp $$
 */
@Slf4j
@Service
public class SgsUserInfoServiceImpl implements SgsUserInfoService {

    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TUserAccountMapper tUserAccountMapper;

    @Autowired
    private SgsUserMerchantDiscountService sgsUserMerchantDiscountService;

    @Override
    public SgsResultResponse<SgsUserInfoQueryResponse> queryUserInfo(SgsUserInfoQueryRequest sgsUserInfoQueryRequest) {
        SgsResultResponse<SgsUserInfoQueryResponse> sgsResponse = new SgsResultResponse<>();

        try {
            String userId = sgsUserInfoQueryRequest.getUserId();
            //查询用户表
            TUserExample example = new TUserExample();
            TUserExample.Criteria criteria =example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            List<TUser> tUsers = this.tUserMapper.selectByExample(example);
            if(CollectionUtils.isEmpty(tUsers)){
                throw new SgpayException(SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseCode(),SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseDesc());
            }
            TUser tUser = tUsers.get(0);
            SgsUserInfoQueryResponse sgsUserInfoQueryResponse = new SgsUserInfoQueryResponse();
            assembleResponse(tUser, sgsUserInfoQueryResponse);

            //查询用户账户表
            TUserAccountExample tUserAccountExample = new TUserAccountExample();
            TUserAccountExample.Criteria tUserAccountExampleCriteria = tUserAccountExample.createCriteria();
            tUserAccountExampleCriteria.andUserIdEqualTo(userId);
            List<TUserAccount> tUserAccounts = this.tUserAccountMapper.selectByExample(tUserAccountExample);
            if(CollectionUtils.isEmpty(tUsers)){
                sgsUserInfoQueryResponse.setAccountAmount(new BigDecimal(0));
                return SgsResponseUtil.INSTANCE.success(sgsUserInfoQueryResponse);
            }
            TUserAccount tUserAccount = tUserAccounts.get(0);
            assembleResponse(sgsUserInfoQueryResponse, tUserAccount);
            sgsResponse = SgsResponseUtil.INSTANCE.success(sgsUserInfoQueryResponse);
        } catch (SgpayException e) {
            log.error("查询用户信息 业务异常：", e);
            sgsResponse.setErrorCode(e.getCode());
            sgsResponse.setErrorMsg(e.getDesc());
        } catch (RuntimeException e) {
            log.error("查询用户信息 异常Exception：", e);
            sgsResponse.setErrorCode(SgsResponseCode.C503000.getResponseCode());
            sgsResponse.setErrorMsg(SgsResponseCode.C503000.getResponseDesc());
        } catch (Exception e) {
            log.error("微查询用户信息 异常Exception：", e);
            sgsResponse.setErrorCode(SgsResponseCode.C503.getResponseCode());
            sgsResponse.setErrorMsg(SgsResponseCode.C503.getResponseDesc());
        } finally {

        }
        return sgsResponse;

    }

    private void assembleResponse(SgsUserInfoQueryResponse sgsUserInfoQueryResponse, TUserAccount tUserAccount) {
        sgsUserInfoQueryResponse.setAccountId(tUserAccount.getUserId());
        sgsUserInfoQueryResponse.setAccountAmount(tUserAccount.getAccountAmount());
        sgsUserInfoQueryResponse.setUserAccountUpdatedAt(tUserAccount.getUpdatedAt());
    }

    private void assembleResponse(TUser tUser, SgsUserInfoQueryResponse sgsUserInfoQueryResponse) {
        sgsUserInfoQueryResponse.setUserId(tUser.getUserId());
        sgsUserInfoQueryResponse.setOpenid(tUser.getOpenid());
        sgsUserInfoQueryResponse.setLoginNo(tUser.getLoginNo());
        sgsUserInfoQueryResponse.setUserName(tUser.getUserName());
        sgsUserInfoQueryResponse.setContact(tUser.getContact());
        sgsUserInfoQueryResponse.setIntroducerId(tUser.getIntroducerId());
        sgsUserInfoQueryResponse.setUserUpdatedAt(tUser.getUpdatedAt());
    }


    /**
     * 查询用户优惠信息列表
     * @param sgsUserInfoQueryRequest
     */
   @Override
   public SgsResultResponse<List<UserMerchantDiscountVo>> queryUserMerchantDiscountListInfo(SgsUserInfoQueryRequest sgsUserInfoQueryRequest){
       //需要补充标准的调用入参
       SgsUserMerchantDiscountListRequest request = new SgsUserMerchantDiscountListRequest();
       request.setLoginChannel(sgsUserInfoQueryRequest.getLoginChannel());
       request.setInitiationID(sgsUserInfoQueryRequest.getInitiationID());
       request.setReqTime(sgsUserInfoQueryRequest.getReqTime());
       request.setUserId(sgsUserInfoQueryRequest.getUserId());
       return sgsUserMerchantDiscountService.queryList( request );
    }



}

